package com.kelvem.saas.workbanch.fsm.entity;

import com.kelvem.saas.workbanch.fsm.core.EntityMappingEnum;
import com.kelvem.saas.workbanch.fsm.core.FsmException;
import lombok.Data;

/**
 * @author kelvem
 */
@Data
public class EntityMapppingNode {
    
    private Class<? extends Entity> leftClazz;
    private String leftKey;
    
    private Class<? extends Entity> rightClazz;
    private String rightKey;
    
    private EntityMappingEnum type;
    private Boolean nullable = true;
    
    public EntityMapppingNode(EntityMappingEnum type, Class<? extends Entity> leftClazz, String leftKey, Class<? extends Entity> rightClazz, String rightKey) {
        this.type = type;
        this.leftClazz = leftClazz;
        this.leftKey = leftKey;
        this.rightClazz = rightClazz;
        this.rightKey = rightKey;
    }
    
    public EntityMapppingNode reverse() {
        EntityMappingEnum type = null;
        if (this.type.equals(EntityMappingEnum.many2one)) {
            type = EntityMappingEnum.one2many;
        } else if (this.type.equals(EntityMappingEnum.one2many)) {
            type = EntityMappingEnum.many2one;
        } else {
            throw new FsmException("不支持翻转对象");
        }
        EntityMapppingNode node = new EntityMapppingNode(type, this.rightClazz, this.rightKey, this.leftClazz, this.leftKey);
        return node;
    }
}
